package com.qianfeng.smsplatform.webmaster.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.smsplatform.webmaster.dao.TApiMappingMapper;
import com.qianfeng.smsplatform.webmaster.dto.DataGridResult;
import com.qianfeng.smsplatform.webmaster.pojo.TApiMapping;
import com.qianfeng.smsplatform.webmaster.pojo.TApiMappingExample;
import com.qianfeng.smsplatform.webmaster.service.ApiMappingService;
import com.qianfeng.smsplatform.webmaster.service.api.CacheService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApiMappingServiceImpl implements ApiMappingService {

    private TApiMappingMapper apiMappingMapper;

    @Autowired
    public void setApiMappingMapper(TApiMappingMapper apiMappingMapper) {
        this.apiMappingMapper = apiMappingMapper;
    }

    @Autowired
    private CacheService cacheService;

    private final String GATEWAY_REDIS_KEY = "APINAME:";

    @Override
    public void addApiMapping(TApiMapping mapping) {
        apiMappingMapper.insert(mapping);
    }

    @Override
    public void updateApiMapping(TApiMapping mapping) {
        apiMappingMapper.updateByPrimaryKey(mapping);
    }

    @Override
    public PageInfo getMappingList(Map criteria, int offset, int limit) {
        PageHelper.offsetPage(offset, limit);
        List<TApiMapping> apiMappingList = apiMappingMapper.getMappingList(criteria);
        PageInfo<TApiMapping> info = new PageInfo<>(apiMappingList);
        return info;
    }

    @Override
    public TApiMapping getMappingById(Integer id) {
        return apiMappingMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteMapping(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return;
        }
        TApiMapping tApiMapping = new TApiMapping();
        tApiMapping.setState((byte) 0);
        TApiMappingExample example = new TApiMappingExample();
        example.createCriteria().andIdIn(Arrays.asList(ids));
        apiMappingMapper.updateByExample(tApiMapping, example);

    }
}
